<template>
  <div class="zh-comment-management">
    <nav-header></nav-header>
    <div class="zh-table">
      <div class="zh-table-open clear">
        <div class="zh-open-left fl">
          <div class="open-width" style="width: 120px">
            <el-select v-model="origin" filterable placeholder="请选择模块">
              <el-option
                v-for="item in originOption"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              ></el-option>
            </el-select>
          </div>
          <div class="open-width" style="width: 270px;">
            <el-input
              v-model="keyword"
              placeholder="请输入评语、评论人、评论来源搜索"
              @keyup.enter.native="searchFunc"
            ></el-input>
          </div>
          <el-button type="primary" @click="searchFunc">搜索</el-button>
          <el-button type="primary" @click="reset">重置</el-button>
        </div>
        <div class="zh-open-right fr"></div>
      </div>
      <div class="zh-table-contents" v-loading="loading.status" :element-loading-text="loading.txt">
        <el-table :data="listData" border max-height="600" style="width: 100%">
          <el-table-column
            type="index"
            :index="typeIndex"
            label="序号"
            align="center"
            show-overflow-tooltip
            width="50"
          ></el-table-column>
          <el-table-column prop="content" label="评语" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="create_name" label="评论人" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="res_name" label="评论来源" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="create_time" label="评论时间" align="center" show-overflow-tooltip></el-table-column>
          <el-table-column prop="5" label="操作" align="center" width="120" show-overflow-tooltip>
            <template slot-scope="scope">
              <el-button @click="checkLogInfo(scope.row)" type="text">查看</el-button>
              <el-button @click="delete_confirm(scope.row)" type="text" class="delete-button">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination" v-show="total_number > 10">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="listParam.page_index"
            :page-sizes="[10, 20, 30, 40, 50, 100]"
            :page-size="listParam.page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total_number"
          ></el-pagination>
          <div class="pagination-button">
            <el-button
              @click="getList"
              type="primary"
              icon="el-icon-refresh"
              circle
              title="刷新数据"
              size="mini"
            ></el-button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      loading: {
        status: false,
        txt: "获取数据..."
      },
      listData: [],
      listParam: {
        origin: null,
        keyword: "",
        page_index: 1,
        page_size: 10
      },
      origin: null,
      keyword: "",
      total_number: 0,
      originOption: [
        {
          value: null,
          label: "全部模块"
        },
        {
          value: 0,
          label: "视频"
        },
        {
          value: 4,
          label: "学科圈组"
        },
        {
          value: 2,
          label: "评课"
        }
      ]
    };
  },
  created() {
    this.getList();
  },
  methods: {
    searchFunc() {
      this.listParam.keyword = this.keyword;
      this.listParam.origin = this.origin;
      this.listParam.page_index = 1;
      this.getList();
    },
    reset() {
      this.keyword = "";
      this.origin = null;
      this.listParam = {
        origin: null,
        keyword: "",
        page_index: 1,
        page_size: 10
      };
      this.getList();
    },
    // 序号
    typeIndex(index) {
      return (
        index + (this.listParam.page_index - 1) * this.listParam.page_size + 1
      );
    },
    checkLogInfo(obj) {
      this.$alert(obj.content, obj.res_name, {
        dangerouslyUseHTMLString: true,
        showConfirmButton: false
      })
        .then(({ value }) => {})
        .catch(() => {});
    },
    getList() {
      this.loading.status = true;
      let success = data => {
        this.loading.status = false;
        this.listData = data.data.data;
        this.total_number = data.data.data_sum;
      };
      let fail = data => {
        this.loading.status = false;
        this.$message.error(data.msg);
      };
      //执行请求，并将回调方法变成config成员以便鉴权过期可以执行
      this.$ajax.get("api/resource/comment", {
        params: this.listParam,
        func: { success: success, fail: fail }
      });
    },
    delete_confirm(obj) {
      let yes = data => {
        this.delete(obj.id, 0, obj.origin);
      };
      let cancel = data => {};
      this.e_confirm({ yes: yes, cancel: cancel, txt: "确定将该评论删除吗？" });
    },
    delete(id, status, origin) {
      let success = data => {
        this.$message.success(data.msg);

        if (this.listData.length - 1 === 0) {
          if (this.listParam.page_index > 1) {
            this.listParam.page_index--;
          } else {
            this.listParam.page_index = 1;
          }
        }

        this.getList();
      };
      this.$ajax.delete("api/resource/comment", {
        params: { id: id, status: status ,origin },
        func: { success: success }
      });
    },
    handleSizeChange(val) {
      this.listParam.page_index = 1;
      this.listParam.page_size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.listParam.page_index = val;
      this.getList();
    }
  }
};
</script>
<style lang="scss">
    .zh-comment-management {
        .el-table__body-wrapper table tbody {
            td:nth-child(6) {
                padding: 0;
            }
        }
    }

    .el-message-box__wrapper {
        .el-message-box__message p {
            word-break: break-word;
        }
    }
</style>